﻿program main
	!本程序提供了一个实现，该实现调用线性方程组求解函数依次求解算例文件夹中的所有方程组，并输出求解耗时和所有线性方程组求解结果的最大残差
	use ReadData, only: readMatrix, readVector
	USE LAPACK95
	use WriteData, only:outputSolution
	use ComputeResidual, only:computeLargestRelResid,computeLargestRelResidOfMultiplier
	use CheckData, only:matrixMatchExplanation
	
implicit none
	!需要根据算例修改的参数
	! character(len=100) :: folderName = "D:\HPCA\OpenAtom_SolAsymDense\StepMatrixRHSAndSolution-3\"!算例的文件夹名（本机地址）
	character(len=100) :: folderName = "C:\zongyi\StepMatrixRHSAndSolution-2\"!算例的文件夹名（云主机地址）
	integer, parameter :: fileNum = 47!算例中的线性方程组个数
	
	
	integer, parameter ::  nTestRounds = 1 !每个线性方程组求解测试的轮数，因为决赛中只用本套数据检查精度，所以此参数始终设置为1
	!与数据文件名相关的变量和参数，不要修改
	character(len=5) :: matrixSuffix = ".matr"
	character(len=4) :: rhsSuffix = ".rhs"
	character(len=100) :: formatDescription
	character(len=100) :: fileNamePrefixM, fileNamePrefixN, fileNamePrefix
	integer, parameter :: fileNamePrefixLength = 5
	
	real(kind=8), dimension(:,:), pointer :: matrix !线性方程组的矩阵
	real(kind=8), dimension(:), pointer :: rhs !线性方程组的右端项
	
	integer :: i,j,k
	
	!用于计时的相关参数
	integer :: count1, count2, countRate 
	integer :: totalCount = 0
	
	!与localMatrix和localRhs相关的参数，定义这些变量是因为gemv会修改传入的矩阵和右端项
	integer :: matrixSize
	real(kind=8), dimension(:,:), allocatable :: localMatrix
	real(kind=8), dimension(:), allocatable :: localRhs
	
	write(formatDescription,'(I1)') fileNamePrefixLength
	formatDescription = '(I'//TRIM(ADJUSTL(formatDescription))//')'
	
	
	! do i = 1, fileNum
	! 	write(fileNamePrefix,formatDescription) i
	! 	do j = 1, fileNamePrefixLength - LEN(TRIM(ADJUSTL(fileNamePrefix)))
	! 		fileNamePrefix  = '0'//ADJUSTL(TRIM(fileNamePrefix))
	! 	end do
		
	! 	call readMatrix(TRIM(ADJUSTL(folderName)) // TRIM(ADJUSTL(fileNamePrefix)),matrixSuffix,matrix)
	! 	call readVector(TRIM(ADJUSTL(folderName)) // TRIM(ADJUSTL(fileNamePrefix)),rhsSuffix,rhs)
		
	! 	matrixSize = size(rhs,1)
	! 	allocate(localMatrix(matrixSize,matrixSize), localRhs(matrixSize))
		
	! 	do j = 1,nTestRounds
	! 		localMatrix(:,:) = matrix(:,:)
	! 		localRhs(:) = rhs(:)
	! 		call SYSTEM_CLOCK(count1)!本实现采用fortran内置的SYSTEM_CLOCK进行计时，会使用统一的计时子程序对所有选手的作品进行评估
	! 		call gesv(localMatrix,localRhs) !本实现调用了mkl 中的gemv来对线性方程组进行求解，后续此部分将会替换成参赛者提交的程序。注意：由于参赛者可能会用到矩阵逐步扩大但前导子矩阵不变的性质，因此程序接口不用完全与gemv一样。
	! 		call SYSTEM_CLOCK(count2)
	! 		totalCount = totalCount + count2 - count1
	! 	end do
		
	! 	call outputSolution(localRhs,folderName,fileNamePrefix)
		
	! 	deallocate (matrix,rhs,localMatrix,localRhs)
		
	! end do
	
	! call SYSTEM_CLOCK(count_rate=countRate)
	! print *, 'Time taken: ', real(totalCount) / real(countRate), ' seconds'
	
	! print *, 'Largest relative residual of the matrixVectorMultiplier: ', computeLargestRelResidOfMultiplier(folderName,fileNum)
	
	print *, 'Largest relative residual of the linearEquationsSolver: ', computeLargestRelResid(folderName,fileNum)
	
	
	pause


end program main
